package com.a.learning_keep.activity.login

import android.content.Intent
import android.widget.Toast
import com.a.learning_keep.base.BaseActivity
import com.a.learning_keep.base.post
import com.a.learning_keep.bean.RegisterData
import com.a.learning_keep.databinding.ActivityRegisterBinding
import com.a.learning_keep.util.showCustomDialog
import kotlin.concurrent.thread

/**
 * 注册账户
 * */
class Register : BaseActivity<ActivityRegisterBinding>() {
    override fun getBind(): ActivityRegisterBinding {
        return ActivityRegisterBinding.inflate(layoutInflater)
    }

    override fun initView() {
        binding.apply {
            outReturnRegister.setOnClickListener { finish() }
            registerBut.setOnClickListener {
                val s1 = binding.phoneUsernameRegister.text.toString()
                val s2 = binding.passwordRegister1.text.toString()
                val s3 = binding.passwordRegister2.text.toString()
                if (s2 != s3) {
                    Toast.makeText(context, "两次密码不相同", Toast.LENGTH_SHORT).show()
                }
                var js = "{\n" +
                    "\"phone\":\"$s1\",\n" +
                    "\"password\":\"$s2\"\n" +
                    "}"
                thread {
                    val data = post("/user/register", js, RegisterData::class.java)
                    runOnUiThread {
                        if (data.message == "success") {
                            showCustomDialog(context, "操作成功，点击确定跳转到登录界面！") {
                                startActivity(
                                    Intent(context, Login::class.java)
                                )
                            }
                        }
                        Toast.makeText(context, data.message, Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    }
}
